// 购物车vuex
import {reqAddToCart,reqGetShopCartList,reqCheckCart,reqDeleteCart,reqCheckCarts,reqDeleteCarts } from "@/api";

//state
const state = {
 shopcartList:[] //购物车列表
}

//mutations
const mutations = {
  //获取购物车数据
  receive_shopcart_list(state,shopcartList){
    state.shopcartList = shopcartList
  }
}

//actions
const actions = {
  //添加到购物车
  async addToCart({commit,dispatch},{skuId,skuNum,flag}){
     const result =  await reqAddToCart(skuId,skuNum)
      // 接口调用成功了还是失败了
      if (result.code !== 200) {
        // 扔出一个错误
        throw new Error(result.message || '添加购物车失败了')
      } 
      flag === true && dispatch('getShopCartList')
  },

  //获取购物车列表数据
  async getShopCartList({commit}){
    const result = await reqGetShopCartList()
    if(result.code !== 200){
        this.$message.error(result.message || '获取购物车数据失败了')
    }else{
      commit('receive_shopcart_list',result.data)
    }

  },

  //切换购物车商品选中状态
  async getCheckCart({dispatch},{skuId,isChecked}){
    const result = await reqCheckCart(skuId,isChecked)
    if(result.code !== 200){
      return result.message || '切换购物车选中状态失败'
    }
    dispatch('getShopCartList')
  },

  //批量切换购物车商品选中状态
  async getCheckCarts({state,dispatch},isCheck){
    //1.获取选中的标识
    const isChecked = isCheck ? 1 : 0
    //2.定义一个数组 --》 判断需要给后端发请求修改的skuId集合
    const skuIdList = []
    state.shopcartList.forEach(cartItem => {
        cartItem.cartInfoList.forEach(item=>{
          if(item.isCheck !== isChecked){
            skuIdList.push(item.skuId)
          }
        })
    });
    //3.调用接口
    const result = await reqCheckCarts(skuIdList,isChecked)
    if(result.code !== 200){
        return result.message || '批量切换购物车选中状态失败'
    }

    //4.再次调用购物车列表接口
    dispatch('getShopCartList')
  },

  //删除购物车商品 -》 后续需要改
  async deleteSingleCart({dispatch},skuId){
    const result = await reqDeleteCart(skuId)
    if(result.code !== 200){
        return result.message || '删除失败'
    }
    dispatch('getShopCartList')
  },

  //批量删除购物车商品-》 后续需要改
  async deleteBatchCart({state,dispatch}){
    //存储要删除的skuid集合
    const skuIdList  = []
    state.shopcartList.forEach(cartItem =>{
      cartItem.cartInfoList.forEach(item=>{
        if(item.isChecked === 1){
          skuIdList.push(item.skuId)
        }
      })
    })
    const result = await reqDeleteCarts(skuIdList)
    if(result.code !== 200){
      return result.message || '批量删除失败'
    }
    dispatch('getShopCartList')
  }

}
//getters
const getters = {

 // 总数量
  totalCount (state) {
    return state.shopcartList.reduce((preTotal, cartItem) => {
      return preTotal + cartItem.cartInfoList.reduce((pre, item) => pre + (item.isChecked === 1 ? item.skuNum : 0), 0)
    }, 0)
  },

  // 总的价格
  totalPrice (state) {
    return state.shopcartList.reduce((preTotal, cartItem) => {
      return preTotal + cartItem.cartInfoList.reduce((pre, item) => pre + (item.isChecked === 1 ? item.skuNum * item.skuPrice : 0), 0)
    }, 0)
  },

  // 是否全选的勾选状态
  isAllChecked (state,getters) {
    return state.shopcartList.every(cartItem=>(cartItem.cartInfoList.every(item=>item.isChecked===1)))&&getters.totalCount>0
  },
}

export default{
  state,
  mutations,
  actions,
  getters
}

